package com.nine.dao;

import java.util.List;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.LockMode;

import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.nine.entity.Addressitem;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Addressitem entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.nine.entity.Addressitem
 * @author MyEclipse Persistence Tools
 */

public class AddressitemDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(AddressitemDAO.class);
	// property constants
	public static final String RECEIVER_NAME = "receiverName";
	public static final String PORT = "port";
	public static final String TELEPHONE = "telephone";
	public static final String LOCAL_OR_OUTLAND = "localOrOutland";
	public static final String ADDRESS = "address";
	
	public static final String CUSTOMER_ID = "customerId";

	protected void initDao() {
		// do nothing
	}

	public boolean save(Addressitem transientInstance) {
		log.debug("saving Addressitem instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			return false;
		}
	}

	public boolean delete(Addressitem persistentInstance) {
		log.debug("deleting Addressitem instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			re.printStackTrace();
			return false;
		}
	}
	
	public boolean update(Addressitem addressitem) {
		try {
			this.getHibernateTemplate().update(addressitem);
			return true;
		} catch (RuntimeException re) {
			re.printStackTrace();
			return false;
		}
	}

	public Addressitem findById(java.lang.Integer id) {
		log.debug("getting Addressitem instance with id: " + id);
		try {
			Addressitem instance = (Addressitem) getHibernateTemplate().get(
					"com.nine.dao.Addressitem", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Addressitem instance) {
		log.debug("finding Addressitem instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Addressitem instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Addressitem as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByReceiverName(Object receiverName) {
		return findByProperty(RECEIVER_NAME, receiverName);
	}

	public List findByPort(Object port) {
		return findByProperty(PORT, port);
	}

	public List findByTelephone(Object telephone) {
		return findByProperty(TELEPHONE, telephone);
	}

	public List findByLocalOrOutland(Object localOrOutland) {
		return findByProperty(LOCAL_OR_OUTLAND, localOrOutland);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}
	
	public List findByCustomer(Object customerId) {
		return findByProperty(CUSTOMER_ID, customerId);
	}

	public List findAll() {
		log.debug("finding all Addressitem instances");
		try {
			String queryString = "from Addressitem";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Addressitem merge(Addressitem detachedInstance) {
		log.debug("merging Addressitem instance");
		try {
			Addressitem result = (Addressitem) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Addressitem instance) {
		log.debug("attaching dirty Addressitem instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Addressitem instance) {
		log.debug("attaching clean Addressitem instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AddressitemDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (AddressitemDAO) ctx.getBean("AddressitemDAO");
	}
}